gc(verbose = FALSE)

##do this first - set your root folder
setwd("/home/ajr7/Replication Archive/")

sink("run.log", split = TRUE)
cat("Starting replication at: ", as.character(Sys.time()), "\n\n")


library(mvtnorm)
library(truncnorm)
library(wnominate)
library(ggplot2)
library(tidyr)
library(dplyr)
library(reshape2)
library(emIRT)
library(doParallel)
library(foreach)
library(rio)
library(RcppEigen)
library(doRNG)
library(doFuture)
library(data.table)
library(Matrix)
library(future.apply)
library(plyr)
library(fields)
library(texreg)
library(lme4)
library(xtable)
library(GGally)
library(stargazer)
library(foreign)


RNGkind("L'Ecuyer-CMRG")
set.seed(1234)


##Notes:
##This archive assumes that you run each file sequentially. 
##The replications of tables and figures (files 5-8) depend
##on having run 1-4 first. These need only be done once.


##this estimates the main model and puts the results EMestimates.Rda in the 
##Results folder
source(paste0(getwd(), "/Replication 1 - Estimate Model.R"))

##this estimates the bootstrap CIs and puts the results EMestimates_boot.Rda in the 
##Results folder
source(paste0(getwd(), "/Replication 2 - Bootstrap CIs.R"))

##this estimates the placebo test and puts the results EMestimates_placebo.Rda in the 
##Results folder
source(paste0(getwd(), "/Replication 3 - Placebo Test.R"))

##this takes the results of the above and merges with various raw data sources
source(paste0(getwd(), "/Replication 4 - Assemble the Data.R"))

##this replicates all main tables in the paper
##they are printed to screen and saved in the Table Output folder
source(paste0(getwd(), "/Replication 5 - Main Paper Tables.R"))

##this replicates all main figures in the paper
##they are printed to screen and saved in the Figure Output folder
source(paste0(getwd(), "/Replication 6 - Main Paper Figures.R"))

##this replicates all appendix tables in the paper
##they are printed to screen and saved in the Table Output folder
source(paste0(getwd(), "/Replication 7 - Appendix Tables.R"))

##this replicates all appendix figures in the paper
##they are printed to screen and saved in the Figure Output folder
source(paste0(getwd(), "/Replication 8 - Appendix Figures.R"))

cat("\nReplication completed at: ", as.character(Sys.time()), "\n")
sink()
